#include "GlobalHead.h"
#include "VtRender.h"
#include "CustomPlayer.h"
#include "App_Application.h"
#include "Command.h"


VtRender::VtRender(QWidget *parent)
:QFrame(parent),mParent(parent)
{
	watch.start();
	ticker.start(25, this);
	setAttribute(Qt::WA_OpaquePaintEvent, true);
	setMouseTracking(false);

	setAcceptDrops(true);

	setFocusPolicy(Qt::StrongFocus);
	
}

VtRender::~VtRender(void)
{
}
void VtRender::renderReset()
{
	CCustomPlayer::Instance().Reset();
}
void VtRender::initialize(HWND mwd, HWND hwd)
{
	CCustomPlayer::Instance().InitPlayer(mwd, hwd);
}

//////////////////////////////////////////////////////////////////////////
void VtRender::newViewport(HWND hwd){
	CCustomPlayer::Instance().initialNewViewport(hwd);
}

void VtRender::closeViewport(int index){
	CCustomPlayer::Instance().closeViewport(index);
}
//////////////////////////////////////////////////////////////////////////

void VtRender::paintEvent(QPaintEvent * event)
{
	CCustomPlayer::Instance().OnPaint();
}

QPaintEngine *VtRender::paintEngine() const
{
  return NULL;
}

void VtRender::timerEvent(QTimerEvent * event)
{
   CCustomPlayer::Instance().Process(0);
}

void VtRender::resizeEvent(QResizeEvent * evn)
{
	HWND wid = this->winId();
	int index = CCustomPlayer::Instance().findWID(wid);
	switch(index){
		//case 0:
		//	CCustomPlayer::Instance().resizeRenderView_index(width(),height(),0);
		//	break;
		case -1:
			break;
		default:
			CCustomPlayer::Instance().resizeRenderView_index(width(),height(),index);
			break;

	}
}
// Qt::DropActions VtRender::supportedDropActions () const
// {
// 	return QStackedWidget::setAcceptDrops(true)
// }
 void VtRender::dragEnterEvent(QDragEnterEvent *event)
 {
 	if( event->mimeData()->hasFormat(QLatin1String("text/x-action-items")))
 	{
 		event->acceptProposedAction();
 	}
 
 }
// void VtRender::dragMoveEvent(QDragMoveEvent *qevent)
// {
// 	QStackedWidget::dragMoveEvent(qevent);
// }
// void VtRender::dragLeaveEvent(QDragLeaveEvent *qevent)
// {
// 	//QStackedWidget::dragLeaveEvent(qevent);
// }
void VtRender::dropEvent(QDropEvent *event)
{
	const QMimeData* mimeData = event->mimeData();

	if (mimeData->hasFormat(QLatin1String("text/x-action-items"))) {

		QByteArray itemData = mimeData->data(QLatin1String("text/x-action-items"));

		QDataStream dataStream(&itemData, QIODevice::ReadOnly);

		int ctActions; dataStream >> ctActions;

		for (int i=0; i<ctActions; i++) {

			QString action;

			dataStream >> action;

			std::string fname = (const char*)action.toAscii();

			//Command::doCommand(Command::App, "App.insertObject()",fname.c_str());
			Command::doCommand((CommandStringFunction)App_Application::importObject,fname.c_str());

		}
	}
}

bool VtRender::eventfilter(QObject *watched, QEvent *event)
 {
    return true;
 }


void VtRender::focusOutEvent(QFocusEvent *event)
{
	return;
}

void VtRender::focusInEvent(QFocusEvent *event)
{
	HWND wid = this->winId();
	int index = CCustomPlayer::Instance().findWID(wid);
	CCustomPlayer::Instance().activateViewport(index);
}

void VtRender::enterEvent(QEvent *)
{
	//CCustomPlayer::Instance()._InputOn();
		//update();
}
void VtRender::leaveEvent(QEvent *)
{
  // CCustomPlayer::Instance()._InputOff();
  // 	update();
}

void VtRender::showEvent(QShowEvent *ev)
{
	CCustomPlayer::Instance().Play();
}
void VtRender::hideEvent(QHideEvent *ev)
{
	CCustomPlayer::Instance().Pause();
}